/*
 *	Copyright © 2013 Changsha Shishuo Network Technology Co., Ltd. All rights reserved.
 *	长沙市师说网络科技有限公司 版权所有
 *	http://www.shishuo.com
 */
package com.shishuo.cms.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shishuo.cms.Directory;
import com.shishuo.cms.constant.ConfigConstant;
import com.shishuo.cms.constant.FolderConstant;
import com.shishuo.cms.constant.FolderConstant.status;
import com.shishuo.cms.dao.PictureDao;
import com.shishuo.cms.entity.Authorization;
import com.shishuo.cms.entity.Config;
import com.shishuo.cms.entity.Folder;
import com.shishuo.cms.entity.Product;
import com.shishuo.cms.entity.vo.FolderVo;
import com.shishuo.cms.entity.vo.JsonVo;
import com.shishuo.cms.entity.vo.PageVo;
import com.shishuo.cms.service.ArticleService;
import com.shishuo.cms.service.AuthorizationService;
import com.shishuo.cms.service.ConfigService;
import com.shishuo.cms.service.ProductService;

/**
 * @author Herbert
 * 
 */
@Controller
@RequestMapping("/folder")
public class FolderAction extends BaseAction {

	@Autowired
	private ArticleService articleService;
	@Autowired
	private ConfigService configService;
	@Autowired
	private ProductService productService;
	@Autowired
	private PictureDao pictureDao;
	final String path="/template/blog/"; 	//返回路劲
	/**
	 * 目录ID
	 * 
	 * @param ename
	 * @param pageNum
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/{folderId}.htm", method = RequestMethod.GET)
	public String fourthFolder(@PathVariable long folderId,
			@RequestParam(value = "p", defaultValue = "1") int p,
			ModelMap modelMap) {
		try {
			String suffix="";//返回路劲后缀
			Folder folder=folderService.getFolderById(folderId);
			long artFolder=folderId;
			long dirId=folderId;
			long newsFolder=folderId;
			if (Directory.NEWS.getType()==folderId||Directory.JOBS.getType()==folderId) {
				List<FolderVo> folderVos=folderService.getFolderListByFatherId(folderId, FolderConstant.status.display);
				if (folderVos.size()>0) {
					artFolder=folderVos.get(0).getFolderId();
				}
				modelMap.put("newList", articleService.getArticlePageByFolderId(p,artFolder));
				suffix="newsList";
			}else if(Directory.NEWS.getType()==folder.getFatherId()||Directory.JOBS.getType()==folder.getFatherId()){
				artFolder=folder.getFolderId();
				dirId=folder.getFatherId();
				newsFolder=folder.getFatherId();
				modelMap.put("newList", articleService.getArticlePageByFolderId(p,artFolder));
				suffix="newsList";
			}else if(Directory.ABOUT.getType()==folderId){
				List<Config> configList = configService.getConfigByKeyList( Arrays.asList(ConfigConstant.ABOUT));
				modelMap.addAttribute("configList", configList);
				suffix="about";
			}else if(Directory.AUTHORIZATION.getType()==folderId){
				suffix="authorization";
			}else if (Directory.FANGWEI.getType()==folderId) {
				dirId=folder.getFatherId();
				suffix="security";
			}else if (Directory.PRODUCT.getType()==folderId||Directory.PRODUCT.getType()==folder.getFatherId()) {
				List<FolderVo> folderVos=new ArrayList<>();
				List<FolderVo> childFolderVos=folderService.getFolderListByFatherId(
						Directory.PRODUCT.getType(), status.display);
				long productFolderId=folderId;
				if (Directory.PRODUCT.getType()==folderId&&childFolderVos.size()==0) {
					productFolderId=Directory.PRODUCT.getType();
				}else if (Directory.PRODUCT.getType()==folderId&&childFolderVos.size()!=0) {
					productFolderId=childFolderVos.get(0).getFolderId();
				}else {
					dirId=Directory.PRODUCT.getType();
				}
				PageVo<Product> pageVo = productService.getProductPage(p,productFolderId);
				for (Product product : pageVo.getList()) {
					product.setPictureList(pictureDao.getPictureByProductId(product.getProductId()));
				}
				if (childFolderVos.size()==0) {
					folderVos.add(folderService.getFolderById(Directory.PRODUCT.getType()));
				}else {
					folderVos.addAll(childFolderVos);
				}
				modelMap.put("folderAll", folderVos);
				modelMap.put("productFolderId", productFolderId);
				modelMap.put("showFolder", Directory.PRODUCT.getType());
				modelMap.put("productList", pageVo);
				suffix="productList";
			}
			List<FolderVo> pathList = folderService
					.getFolderPathListByFolderId(artFolder);
			if (Directory.JOBS.getType()==newsFolder) {
				modelMap.put("param", Directory.JOBS.toString().toLowerCase());
			}
			modelMap.put("pathList", pathList);
			modelMap.put("newFolder", newsFolder);
			modelMap.put("folder", folder);
			modelMap.put("g_folderId", dirId);
			modelMap.put("news_actId", artFolder);
			modelMap.put("p", p);
			return path+suffix;
		} catch (Exception e) {
			modelMap.addAttribute("g_folderId", folderService.getFolderByOrderAndFatherId(0).getFolderId());
			logger.fatal(e.getMessage());
			return themeService.get404();
		}
	}
	
	@Autowired
	private AuthorizationService authorizationService;
	@ResponseBody
	@RequestMapping(value = "/getAuthorization.json", method = RequestMethod.GET)
	public JsonVo<String> getAuthorization(ModelMap modelMap,
			@RequestParam(value = "condition") String condition)
			throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		Authorization authorization = authorizationService.selectByCondition(condition);
		if (authorization==null) {
			json.setResult(false);
		}else {
			json.setResult(true);
			json.setMsg(authorization.getUrl());
		}
		return json;
	}
}
